//
//  StudioContactViewController.m
//  Arts1
//
//  Created by Mac Jc on 15/9/28.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "StudioContactViewController.h"
#import "AllHeader.h"

// 画室   联系我们   数据接口
#define  conURL @"http://arts1.dlab.com.cn/app/contactus"

@interface StudioContactViewController ()<UIWebViewDelegate>


//  展示内容
@property (nonatomic, strong) NSString *textString;

// 滑动视图
@property (nonatomic, strong) UIScrollView *scrollview;

// 展示视图
@property (nonatomic, strong) UIWebView *webView;

@end

@implementation StudioContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackgroundColor;
    // Do any additional setup after loading the view.

    [self createcontactView];
  
}

- (void)setSpaceId:(NSString *)spaceId{
    _spaceId = spaceId;
    if (_spaceId) {
        [self requestData];
    }
}

//请求数据
- (void)requestData
{
    
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *pardic = @{@"spaceId":_spaceId};
    
    [manage GET:conURL parameters:pardic
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            // 请求成功的时候调用这个block
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            // 请求返回的状态，当status = 1 时有数据
            NSInteger status = [dic[@"status"] integerValue];
            if (status == 1) {
             NSString *str = dic[@"data"];
              _textString = [NSString htmlEntityDecode:str];
                
                
              //  _textString = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                
                NSLog(@"课程联系我们   富文本   _textString: %@",_textString);
                
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
              _textString = [NSString importStyleWithHtmlString:_textString];
                
            [_webView loadHTMLString:_textString baseURL:nil];
                
            });
            NSLog(@"课程联系我们 ===数据请求--%@", dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // 请求失败的时候调用调用这个block
            NSLog(@"请求失败");
        }];
    
}

//  创建联系我们页面
- (void)createcontactView{
    /**
     滑动视图
     */
//    _scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 213)];
//    _scrollview.backgroundColor =kCustomColor(245,245, 245, 1);
//    _scrollview.contentSize = CGSizeMake(0, self.view.height);
//    _scrollview.alwaysBounceVertical = YES;
//    
//    /**
//     内容展示视图
//     */
//    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 25, SCREEN_WIDTH - 20, 360)];
//    _webView.userInteractionEnabled = YES;
//    _webView.delegate = self;
//    _webView.backgroundColor = [UIColor whiteColor];
//    [UIView viewShadowColor:_webView];
//    
//    /**
//     彩带视图
//     */
//    UIImageView *mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -15, SCREEN_WIDTH - 20, 10)];
//    mainImageView.backgroundColor = [UIColor whiteColor];
//    mainImageView.image = [UIImage imageNamed:@"colorImage"];
//    [_webView addSubview:mainImageView];
//    
//    [_scrollview addSubview:_webView];
//    [self.view addSubview:_scrollview];
    
    _scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 213)];
    _scrollview.backgroundColor = kBackgroundColor;
    _scrollview.contentSize = CGSizeMake(0, self.view.height);
    _scrollview.alwaysBounceVertical = YES;
    
    UIView *topShadowView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 12.5)];
    topShadowView.backgroundColor = [UIColor whiteColor];
    topShadowView.layer.cornerRadius = 5;
    //    [UIView viewWideForShadowColor:topShadowView];
    [_scrollview addSubview:topShadowView];
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(10,15, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 138)];
    _webView.userInteractionEnabled = YES;
    _webView.delegate = self;
    _webView.backgroundColor = [UIColor whiteColor];
    [UIView viewShadowColor:_webView];
    [_scrollview addSubview:_webView];
    
    
    UIImageView *mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 10)];
    mainImageView.backgroundColor = [UIColor whiteColor];
    mainImageView.image = [UIImage imageNamed:@"colorImage"];
    [UIView viewRediusForLeftAndReightOfTop:mainImageView withRedius:5];
    [_scrollview addSubview:mainImageView];
    
    
    [self.view addSubview:_scrollview];
}

#pragma mark ---- UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView{

    //通过JS命令获取加载完成的webView的高度
    CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    
    //重新设置webView的高度
    _webView.frame = CGRectMake(10, 20 , SCREEN_WIDTH - 20, height + 30 + 500);
    _scrollview.contentSize = CGSizeMake(0, 25 + height + 30);
    
}

// 返回上一层
- (void)back:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
     self.tabBarController.tabBar.hidden = NO;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
